package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.alarm.Alarm;
import com.ideal.manage.esms.bean.project.AccountProperty;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.service.alarm.AlarmService;
import com.ideal.manage.esms.service.monitor.MonitorBlockChartService;
import com.ideal.manage.esms.service.monitor.MonitorBlockService;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.ProjectManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/5/25.
 */
@Controller
@RequestMapping("monitor")
public class MonitorForBlockController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private ProjectManageService projectManageService;
    @Resource
    private AlarmService alarmService;
    @Autowired
    private MonitorBlockService monitorBlockService;
    @Autowired
    private MonitorBlockChartService monitorBlockChartService;

    /**
     * 系统监控内嵌页面 - 项目
     *
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_block")
    public void monitorWebForBlock(Model model, String id) {
        // 1. 标题栏
        model.addAttribute("id", id);
        ProjectManage projectManage = projectManageService.findOne(id);
        model.addAttribute("projectManage", projectManage);

        // 2. 实时监控 - 实时概况
        List<ParameterInfo> parameterInfoList = monitorBlockService.getRealTimeInfo(projectManage.getIp());
        model.addAttribute("parameterInfoList", parameterInfoList);
        // 3. 实时监控 - 电池包总览
        List<JSONObject> equipmentManageList = monitorBlockService.getBatteryPack(projectManage.getId());
        model.addAttribute("equipmentManageList", equipmentManageList);
        //项目信息
        List<AccountProperty> accountPropertyListForProject = monitorService.accountPropertyList(null, 1, projectManage);
        model.addAttribute("accountPropertyListForProject", accountPropertyListForProject);
        //系统信息
        List<AccountProperty> accountPropertyListForSystem = monitorService.accountPropertyList(null, 4, projectManage);
        model.addAttribute("accountPropertyListForSystem", accountPropertyListForSystem);
        //电价信息
        List<AccountProperty> accountPropertyListForElectric = monitorService.accountPropertyList(null, 6, projectManage);
        model.addAttribute("accountPropertyListForElectric", accountPropertyListForElectric);
        //报警等级数量
        List<Alarm> alarmList = monitorBlockService.findAlarmEquipment(id);
        JSONObject level = alarmService.findLevelByAlarm(alarmList);
        model.addAttribute("level", level);

    }

    /**
     * 实时监控 今日充放电量、电费曲线
     *
     * @param id
     * @return
     */
    @RequestMapping("today_electricity")
    @ResponseBody
    public Map todayElectricity(String id) {
        return monitorBlockChartService.getTodayElectricity(id);
    }

    /**
     * 系统监控页面 - 楼宇级
     * 实时监控 - 故障信息
     *
     * @param id
     * @return
     */
    @RequestMapping("error_data")
    @ResponseBody
    public List<Alarm> errorData(String id, Model model) {
        List<Alarm> alarmList = monitorBlockService.findAlarmEquipment(id);
        return alarmList;
    }
}
